package com.lib.online.service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.lib.online.dao.LibDAO;
import com.lib.online.dao.jdbc.LibDaoHbrImpl;
import com.lib.online.model.Author;
import com.lib.online.model.Reading;




/**
 * @author ievgen_s
 */
@Service
public class BookService {

	@Resource(type=LibDaoHbrImpl.class)
    private LibDAO connector;   
   
    public BookService() {}

    public void addBook(Reading book) {

	connector.addBook(book);
    }

    public List<Reading> getAllBooks() {

	return connector.getAllBooks();
    }
    
    public Reading getBook(String title) {

   	return connector.getBook(title);
       }
    
    public void uploadBook(Reading newBook) {
	
		  try {
		 connector.addBook(newBook);
		  } catch (Exception e) {	
		  }
	
    }

    public Reading randomBook() {

	List<Reading> rand = new ArrayList<Reading>();
	rand.addAll(connector.getAllBooks());	
	return rand.get(new Random().nextInt(rand.size()));

    }

    public List<Reading> nameSortedBooks() {

	List<Reading> namesort = new ArrayList<Reading>();
	namesort.addAll(connector.getAllBooks());
	Collections.sort(namesort);
	return namesort;

    }
    
    public List<Author> getAuthors() {

	return connector.getAuthors();
    }

   
}